/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.service.dic;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.boxin.ims.modules.wechat.dao.dic.StorefrontDao;
import com.boxin.ims.modules.wechat.entity.dic.Address;
import com.boxin.ims.modules.wechat.entity.dic.Storefront;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;

/**
 * 实体店Service
 * @author Jakemanse
 * @version 2013-12-10
 */


@Component
@Transactional(readOnly = true)
public class StorefrontService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(StorefrontService.class);
	
	@Autowired
	private StorefrontDao storefrontDao;
	
	public Storefront get(Long id) {
		return storefrontDao.findOne(id);
	}
	
	
	public List<Storefront>  findByXY(Long userId,Address address){
		DetachedCriteria dc = storefrontDao.createDetachedCriteria();
		
		dc.add(Restrictions.eq("user.id", userId));		//用户
		
		if (address!=null && address.getCity()!=null && com.thinkgem.jeesite.common.utils.StringUtils.isNotEmpty(address.getCity())){
			dc.add(Restrictions.like("city", "%"+address.getCity()+"%"));
		}
		
		
		if (address!=null && address.getProvince()!=null && com.thinkgem.jeesite.common.utils.StringUtils.isNotEmpty(address.getProvince())){
			dc.add(Restrictions.like("province", "%"+address.getProvince()+"%"));
		}
		
		
		dc.addOrder(Order.desc("id"));
		return storefrontDao.find( dc);
	}
	
	
	/**
	 * @author Jakemanse
	 * @time 2013-12-12  上午12:51:17
	 * @function <p> 根据核销指令查找店面 </p>
	 * @param command
	 * @return
	 */
	public Storefront getByCommand(String command){
		return storefrontDao.getByCommand(command);
	}
	
	
	public Page<Storefront> find(Page<Storefront> page, Storefront storefront) {
		DetachedCriteria dc = storefrontDao.createDetachedCriteria();
		if (storefront.getUser()!=null && storefront.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", storefront.getUser().getId()));
		}
		
		
		if (StringUtils.isNotEmpty(storefront.getName())){
			dc.add(Restrictions.like("name", "%"+storefront.getName()+"%"));
		}
		
		
		if (StringUtils.isNotEmpty(storefront.getProvince())){
			dc.add(Restrictions.like("province", "%"+storefront.getName()+"%"));
		}
		
		
		if (StringUtils.isNotEmpty(storefront.getCity())){
			dc.add(Restrictions.like("city", "%"+storefront.getCity()+"%"));
		}
		
		
		
		dc.addOrder(Order.desc("id"));
		return storefrontDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public void save(Storefront storefront) {
		storefrontDao.save(storefront);
	}
	
	
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		storefrontDao.deleteById(id);
	}
	
	
	
}
